<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\DB;
use Laravel\Passport\HasApiTokens;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
use App\Notifications\ResetPasswordNotification;
use App\Common\ConstCode;
class Picture extends Authenticatable
{

    use HasApiTokens,Notifiable;


    protected $table = 'cartoon_picture';

    public    $timestamps = false;
    protected $primaryKey = 'id';


    public static function lists($data){
        $where=[
            'type'=>$data['type'],
            'sid'=>$data['sid']
        ];
        if(isset($data['location'])) $where['location']=$data['location'];
        $result=DB::table('cartoon_picture')->where($where)->select(['name','pic_uri','front_hair','after_hair','created_at','id','location'])
            ->forPage($data['page'], $data['pageSize'])
            ->get()
            ->toArray();
        foreach ($result as &$val){
            $val->pic_uri=uri_fill($val->pic_uri?$val->pic_uri:'/cartoon/default.png');
            $val->front_hair=uri_fill($val->front_hair?$val->front_hair:'/cartoon/default.png');
            $val->after_hair=uri_fill($val->after_hair?$val->after_hair:'/cartoon/default.png');
        }

        $curCount=DB::table('cartoon_picture')->where($where)->count();

        if($data['type']==2 && !$result){
            $result[0]['pic_uri']=uri_fill('/cartoon/default.png');
            $curCount=1;
        }
        $data = [
            'list'   =>  $result,
            'total' =>  $curCount,
        ];
        return $data;
    }


    public static function bubble_lists(array $data):array {
        $category=DB::table('bubble')->where('id',$data['sid'])->value('category');
        $where=['category'=>$category,'is_shelf'=>1,'status'=>1];
        $lists=DB::table('bubble')->where($where)
            ->select('id','pic_uri','type')
            ->forPage($data['page'], $data['pageSize'])
            ->orderBy("id","asc")
            ->get()
            ->toArray();
        if($lists){
            foreach ($lists as &$val){
                $val->pic_uri=uri_fill($val->pic_uri);
            }
        }

        $curCount=DB::table('bubble')->where($where)->count();
        $data = [
            'list'   =>  $lists,
            'total' =>  $curCount,
        ];

        return $data;
    }


}
